<?php
class TurnoverModel extends Model{
	public function getTypeByAction($action){
		$where=array(
			'action'=>$action
		);
		$type_list=M('turnover_type')->where()->select();
		return $type_list;
	}
	
	public function addMoney($data){
		M('turnover_money')->add($data);
	}
	public function addTotal($data){
		M('turnover_total')->add($data);
	}
	public function getTotalByTime($add_time){
		
		$total_id=M('turnover_total')->where(array('add_time'=>$add_time))->getField('total_id');
		return $total_id;
	}
	public function getTotalByDate($d1,$d2,$action){
		$list=M('turnover_total')->where("status=1 AND type='{$action}' AND total_date>={$d1} AND total_date<{$d2}")->select();
		foreach($list as $k=>$v){
			$dep=$this->getDepartment($v['dep']);
			$list[$k]['depname']=$dep['name'];
		}
		return $list;
	}
	public function getExpend($total_id){
		$list=array();
		$where=array(
		'total_id'=>$total_id
		);
		$total=M('turnover_total')->where($where)->find();
		$total['department']=$this->getDepartment($total['dep']);
		$money_list=M('turnover_money')->where($where)->select();
		
		$where_type=array(
		'action'=>$total['type']
		);
		$type_list=M('turnover_type')->where($where_type)->select();
		$list['total']=$total;
		foreach($money_list as $key=>$vo){
			$list['money'][$vo['tid']]=$vo;
		}
		foreach($type_list as $key=>$vo){
			if($vo['parent_id']==0){
				$list['type'][$vo['tid']]=$vo;
			}else{
				$list['type'][$vo['parent_id']]['power'][$vo['tid']]=$vo;
			}
			
		}
		
		return $list;
	}
	public function getTableList(){
		$expend=M('turnover_total')->where(array('type'=>"expend"))->select();
		$income=M('turnover_total')->where(array('type'=>"income"))->select();
		if(count($expend)>0){
			foreach($expend as $vo){
				$department=M('department')->where(array('department_id'=>$vo['dep']))->find();
				$expend[$key]['department']=$department;
			}
		}
		if(count($income)>0){
			foreach($income as $vo){
				$department=M('department')->where(array('department_id'=>$vo['dep']))->find();
				$income[$key]['department']=$department;
			}
		}
		$list['expend']=$expend;
		$list['income']=$income;
		return $list;
	}
	public function getDepartment($dep){
		$where=array(
		'department_id'=>$dep
		);
		$department=M('department')->where($where)->find();
		return $department;
	}
	public function getDepartment2(){
		$where=array(
		'parent_id'=>0
		);
		$super=M('department')->where($where)->find();
		$parent=M('department')->where(array('parent_id'=>$super['department_id']))->select();
		$list['super']=$super;
		
		foreach($parent as $k=>$v){
			$child=M('department')->where(array('parent_id'=>$v['department_id']))->select();
			$list['p'][$k]['parent']=$v;
			$list['p'][$k]['child']=$child;
		}
		
		return $list;
	}
	public function getTotalByType($type){
		$where=array(
			'type'=>$type,
			'status'=>1
		);
		$list=M('turnover_total')->where($where)->select();
		foreach($list as $k=>$v){
			$dep=$this->getDepartment($v['dep']);
			$list[$k]['depname']=$dep['name'];
		}
		return $list;
	}
	
	public function getTableByDate($d1,$d2){
		//按日期取出营业额
		$to_list=M('turnover_total')->where("status=1 AND type='to' AND total_date>={$d1} AND total_date<{$d2}")->select();
		$sum=0;
		foreach($to_list as $k=>$v){
			if(empty($todep[$v['dep']]['sum'])){$todep[$v['dep']]['sum']=$v['money_sum'];}
			else{
				$todep[$v['dep']]['sum']+=$v['money_sum'];
			}
		}
		foreach($todep as $k=>$v){
			$dname=$this->getDepartment($k);
			$todep[$k]['depname']=$dname['name'].$v['sum'];
		}
		//按日期取出支出总数
		$ex_list=M('turnover_total')->where("status=1 AND type='expend' AND total_date>={$d1} AND total_date<{$d2}")->select();
		$types=M('turnover_type')->where("action='expend'")->select();
		foreach($types as $k=>$v){
			if($v['parent_id']==0){
				$type_list[$v['tid']]['parent']=$v['name'];
			}else{
				$type_list[$v['parent_id']]['child'][$v['tid']]['sum']=0;
				$type_list[$v['parent_id']]['child'][$v['tid']]['name']=$v['name'];
				$t[$v['tid']]=0;
				
			}
		}
		$sum=0;
		foreach($ex_list as $k=>$v){
			//表格支出数据
			$moneys=M('turnover_money')->field('tid,sum(money)')->where("total_id={$v['total_id']}")->group('tid')->select();
			foreach($moneys as $m=>$s){
					$t[$s['tid']]+=$s['sum(money)'];
			}
			
			if(empty($exdep[$v['dep']]['sum'])){$exdep[$v['dep']]['sum']=$v['money_sum'];}
			else{
				$exdep[$v['dep']]['sum']+=$v['money_sum'];
			}
		}
		foreach($type_list as $k=>$v){
			foreach($v['child'] as $kk=>$vo){
				$type_list[$k]['child'][$kk]['sum']=$t[$kk];
			}
			
		}
		//柱状图数据
		foreach($exdep as $k=>$v){
			$dname=$this->getDepartment($k);
			$exdep[$k]['depname']=$dname['name'];
		}
		//按日期取出收入总数
		$in_list=M('turnover_total')->where("status=1 AND type='income' AND total_date>={$d1} AND total_date<{$d2}")->select();
		$sum=0;
		foreach($in_list as $k=>$v){
			if(empty($indep[$v['dep']]['sum'])){$indep[$v['dep']]['sum']=$v['money_sum'];}
			else{
				$indep[$v['dep']]['sum']+=$v['money_sum'];
			}
		}
		foreach($indep as $k=>$v){
			$dname=$this->getDepartment($k);
			$indep[$k]['depname']=$dname['name'].$v['sum'];
		}
		//将三个总数存入list返回
		$list['todep']=$todep;
		$list['exdep']=$exdep;
		$list['indep']=$indep;
		$list['type_list']=$type_list;
		return $list;
//		$ex_list=M('turnover_total')->where("status=1 AND type='expend' AND total_date>={$d1} AND total_date<{$d2}")->select();
//		foreach($ex_list as $k=>$v){
//			$ex_list[$k]['depname']=$this->getDepartment($v['dep']);
//		}
//		$in_list=M('turnover_total')->where("status=1 AND type='income' AND total_date>={$d1} AND total_date<{$d2}")->select();
//		foreach($ex_list as $k=>$v){
//			$in_list[$k]['depname']=$this->getDepartment($v['dep']);
//		}
	}
	public function getExtableByM($d1,$d2,$action){
		
		dump($total_list);
		$type_list=M('turnover_type')->where(array("action"=>$action))->order('parent_id')->select();
		$bigtype=M('turnover_type')->where("parent_id=0 AND action='expend'")->select();
		
		
		$days=date("t",strtotime($m));
		$mm=date("Y-m",$m)."-".$days;
		$d1=$m;
		$d2=strtotime($mm);
//		echo date("Y-m-d H:i",$d1);
//		echo date("Y-m-d H:i",$d2);
		$wh=array(
			'total_date'=>[['>=',$d1],['<',$d2],'and'],
			'status'=>1
			);
		$total_list=M('turnover_total')->where("status=1 AND total_date>{$d1} AND total_date<{$d2}")->select();
		$list=array();
		//小分类
		foreach($type_list as $t=>$p){
			if($p['parent_id'] !=0){
				$list[$p['tid']]['name']=$p['name'];
				$list[$p['tid']]['money']=0;
				$list2[$p['parent_id']][$p['tid']]['name']=$p['name'];
				$list2[$p['parent_id']][$p['tid']]['money']=0;
			}	
		}
		//取出所有小分类的金额
		foreach($total_list as $k=>$vo){
			$where=array(
				'total_id'=>$vo['total_id'],
			);
			$mn=M('turnover_money')->where($where)->select();
			foreach($mn as $m=>$s){
				$list[$s['tid']]['money']+=$s['money'];	
				$list2[$p['parent_id']][$p['tid']]['money']+=$s['money'];
			}	
		}
		//大分类
		foreach($bigtype as $k=>$v){
			
			$sum=0;
			$smalltype=M('turnover_type')->where("parent_id={$v['tid']}")->select();
			foreach($smalltype as $m=>$s){
				if(!empty($list[$s['tid']]['money'])){
					$sum+=$list[$s['tid']]['money'];
				}
			}
			$big_list[$v['tid']]['sum']=$sum;
			$big_list[$v['tid']]['name']=$v['name']."*".$sum;
		}
		
		$all['big_list']=$big_list;
		$all['small_list']=$list;
		$all['small_list2']=$list2;
		return $all;	
	}
	public function getTotalByTo($m){
		$days=date("t",strtotime($m));
		$mm=date("Y-m",$m)."-".$days;
		$d1=$m;
		$d2=strtotime($mm);
		$list=M("turnover_total")->field('dep,sum(money_sum)')->where("type='to' AND total_date>{$d1} AND total_date<{$d2}")->group('dep')->select();
		foreach($list as $k=>$v){
			$depname=$this->getDepartment($v['dep']);
			$list[$k]['depname']=$depname['name']."*".$v['sum(money_sum)'];
		}
	return $list;
	}
	
	
	
	
	
	
	
}
?>